# coding=utf-8
# Copyright (C) xxx team - All Rights Reserved
#
# @Version:   3.9.4
# @Software:  PyCharm
# @FileName:  get_data.py
# @CTime:     2021/6/4 14:30   
# @Author:    xxx
# @Email:     xxx
# @UTime:     2021/6/4 14:30
#
# @Description:
#     xxx
#     xxx
#
import codecs
import logging
from typing import List, Dict, Optional
import json
from pymongo import MongoClient

logger = logging.getLogger(__name__)


def read_mongdb(address='172.31.96.191', db_name='migu_kg', collection_name='video'):
    client = MongoClient(host=address,
                         port=27017,
                         username='admin',
                         password='admin',
                         authMechanism='SCRAM-SHA-1')
    db = client[db_name]
    collection = db.get_collection(collection_name)

    return collection


def main(output_file='output_data.txt'):
    collection = read_mongdb()
    with codecs.open(output_file, 'w', 'utf-8') as f:
        for i, l in enumerate(collection.find(), 1):
            line = {
                'video_name': l['name'],
                'category': l['category'],
            }
            f.writelines(json.dumps(line, ensure_ascii=False))
            f.writelines('\n')

            if i == 10:
                break

if __name__ == '__main__':
    main()
